﻿//
//                       _oo0oo_
//                      o8888888o
//                      88" . "88
//                      (| -_- |)
//                      0\  =  /0
//                    ___/`---'\___
//                  .' \\|     |// '.
//                 / \\|||  :  |||// \
//                / _||||| -:- |||||- \
//               |   | \\\  -  /// |   |
//               | \_|  ''\---/''  |_/ |
//               \  .-\__  '-'  ___/-. /
//             ___'. .'  /--.--\  `. .'___
//          ."" '<  `.___\_<|>_/___.' >' "".
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//         \  \ `_.   \_ __\ /__ _/   .-` /  /
//     =====`-.____`.___ \_____/___.-`___.-'=====
//                       `=---='
//
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//               佛祖保佑         永无BUG
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#region << 版 本 注 释 >>

/*----------------------------------------------------------------
* 类 名 称 ：HUDTextMgr
* 类 描 述 ：
* 作    者 ：henrychang
* 创建时间 ：2019/1/25 14:52:58
*******************************************************************
* Copyright @ henrychang 2018. All rights reserved.
*******************************************************************
//----------------------------------------------------------------*/

#endregion << 版 本 注 释 >>

using System.Collections.Generic;
using UnityEngine;

namespace TouchFish.UI
{
    public class HUDTextMgr : MonoBehaviour
    {
        public const string TemplateTextPrefab = "UI/TemplateTextPrefab";

        public static HUDTextMgr _instance;

        public static HUDTextMgr Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = UIManager.Instance.RootCanvas.gameObject.AddComponent<HUDTextMgr>();
                    _instance.TextParent = UIManager.Instance.PopUI;
                    _instance.TextPrefab = Resources.Load<GameObject>(TemplateTextPrefab);
                }
                return _instance;
            }
        }

        /// <summary>
        /// The Canvas Root of scene.
        /// </summary>
        [Tooltip("Canvas Root of scene.")]
        public Transform TextParent;

        /// <summary>
        /// 默认的UI预制体
        /// </summary>
        public GameObject TextPrefab;

        #region 创建Text

        public HUDText NewText(string text, Transform trans)
        {
            return NewText(text, trans, Color.white);
        }

        public HUDText NewText(string text, Transform trans, Color color)
        {
            return NewText(text, trans, color, 8);
        }

        public HUDText NewText(string text, Transform trans, Color color, int size)
        {
            return NewText(text, trans, color, size, null);
        }

        private HUDText NewText(string text, Transform trans, Color color, int size, GameObject textPrefab)
        {
            GameObject prefab = (textPrefab == null) ? TextPrefab : textPrefab;
            //Create new text info to instantiate  TODO:需要使用对象池进行优化
            GameObject go = Instantiate(prefab) as GameObject;
            HUDText item = go.GetComponent<HUDText>();
            item.TextContent = text;
            item.TargetTransform = trans;
            item.Color = color;
            item.Size = size;
            go.transform.SetParent(TextParent, false);
            go.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
            return item;
        }

        #endregion 创建Text

        private Vector2 ScreenPosition(Transform t)
        {
            return Vector2.zero;
        }
    }
}